package com.tj.jinzao.activity;


import android.Manifest;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.widget.ListView;

import com.tj.jinzao.R;
import com.tj.jinzao.adapter.QuestionAdapter;
import com.tj.jinzao.adapter.QuestionListAdapter;
import com.tj.jinzao.api.ApiRetrofit;
import com.tj.jinzao.api.ApiService;
import com.tj.jinzao.api.SubscriberCallBack;
import com.tj.jinzao.base.BaseActivity;
import com.tj.jinzao.model.QuestionModelBean;
import com.tj.jinzao.model.ResultResponse;

import java.util.HashMap;
import java.util.List;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * 客服中心页面
 * Created by wjy on 2019/1/5.
 */
public class QuestionActivity extends BaseActivity {
    protected ApiService mApiService = ApiRetrofit.getInstance().getApiService();

    private CompositeSubscription mCompositeSubscription;
    private QuestionListAdapter questionAdapter;
    private ListView custom_question;


    public void detachView() {
        onUnsubscribe();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        detachView();
    }

    @Override
    public int getLayoutResource() {
        return R.layout.activity_question;
    }

    @Override
    protected int getLeftButtonResId() {
        return R.mipmap.ic_back;
    }

    @Override
    protected void findId() {
        custom_question=findViewById(R.id.custom_question);
    }

    @Override
    protected void setupView() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.CALL_PHONE},
                    1);
        }
        getQuestion();
    }
    public void getQuestion() {
        HashMap<String,Object> map=new HashMap<>();
        map.put("firstDirId",String.valueOf(getIntent().getIntExtra("id",0)));
        addSubscription(mApiService.getMULTWO(map), new SubscriberCallBack<List<QuestionModelBean>>(this) {
            @Override
            protected void onSuccess(List<QuestionModelBean> list) {
                questionAdapter=new QuestionListAdapter(QuestionActivity.this,list,false);
                custom_question.setAdapter(questionAdapter);
            }

            @Override
            protected void onFailure(ResultResponse response) {

            }

            @Override
            protected boolean isShowProgress() {
                return false;
            }

            @Override
            protected void onPageSuccess(int currentPage, int allPage, int allSize) {
            }
        });

    }

    public void addSubscription(Observable observable, Subscriber subscriber) {
        if (mCompositeSubscription == null) {
            mCompositeSubscription = new CompositeSubscription();
        }
        mCompositeSubscription.add(observable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber));
    }

    //RXjava取消注册，以避免内存泄露
    public void onUnsubscribe() {
        if (mCompositeSubscription != null && mCompositeSubscription.hasSubscriptions()) {
            mCompositeSubscription.unsubscribe();
        }
    }

}
